document.addEventListener('DOMContentLoaded', function() {
    // 显示操作结果消息
    function showAlert(message, isSuccess) {
        const alertType = isSuccess ? 'success' : 'danger';
        const alertDiv = document.createElement('div');
        alertDiv.className = `alert alert-${alertType} alert-dismissible fade show`;
        alertDiv.innerHTML = `
            <span>${message}</span>
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        `;

        const container = document.querySelector('.container') || document.body;
        container.prepend(alertDiv);

        setTimeout(() => {
            alertDiv.classList.remove('show');
            setTimeout(() => alertDiv.remove(), 150);
        }, 3000);
    }

    // 检查并显示页面加载时的Flash消息
    const successMessage = document.querySelector('.alert-success');
    const errorMessage = document.querySelector('.alert-danger');

    if (successMessage) {
        showAlert(successMessage.textContent, true);
    }
    if (errorMessage) {
        showAlert(errorMessage.textContent, false);
    }

    // 处理表单提交方法转换
    document.querySelectorAll('[data-method="post"]').forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();

            const actionText = this.textContent.trim();
            const confirmMessage = `确定要${actionText}该职位吗？`;

            if (confirm(confirmMessage)) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = this.href;
                document.body.appendChild(form);
                form.submit();
            }
        });
    });
});